<?php

namespace app\master\controller;

use think\Exception;

class Banner extends Base
{
    //banner列表
    public function banner()
    {
        if (request()->isGet()) {
            return $this->fetch("banner");
        }
        $where = [
            "banner_status" => 1
        ];
        $field = "banner_id,banner_path,banner_status,FROM_UNIXTIME(banner_time) as banner_time";
        $list = db("Banner")->where($where)->field($field)->order("banner_id desc")->select();
        $count = db("Banner")->where($where)->count();
        $this->tableReturn(0, "success", $count, $list);
    }

    //删除轮播
    public function delbanner()
    {
        $where = [
            "banner_id" => input("banner_id")
        ];
        $bannerModel = db("Banner");
        $status = $bannerModel->where($where)->value("banner_status");
        if ($status != 1) $this->ajaxReturn(603, errorInfo, url("Banner/banner"));
        $update = $bannerModel->where($where)->setField("banner_status", 3);
        if (!$update) $this->ajaxReturn(603, errorMsg);
        $this->ajaxReturn(200, successMsg);
    }

    //修改轮播图
    public function updbanner()
    {
        if (request()->isGet()) {
            $where = ["banner_id" => input("banner_id")];
            $info = db("Banner")->where($where)->find();
            $this->assign("info", $info);
            return $this->fetch("updbanner");
        }
        $data = input("post.");
        $oldPath = config("TEMPORARY_CATALOG_FILE_PATH") . getFileName($data["img"]);
        $newPath = config("BANNER");
        $path = moveFile($oldPath, $newPath);
        if (!$path) $this->ajaxReturn(603, selectFile);
        $upd["banner_path"] = $path;
        $update = db("Banner")->where('banner_id', $data['banner_id'])->update($upd);
        if (!$update) $this->ajaxReturn(603, successMsg);
        $this->ajaxReturn(200, successMsg, url('Banner/banner'));
    }

    //添加轮播图
    public function addbanner()
    {
        if (request()->isGet()) {
            return $this->fetch("addbanner");
        }
        $data = input("post.");
        $oldPath = config("TEMPORARY_CATALOG_FILE_PATH") . getFileName($data["img"]);
        $newPath = config("BANNER");
        $path = moveFile($oldPath, $newPath);
        if (!$path) $this->ajaxReturn(603, selectFile);
        $where["banner_path"] = $path;
        $where["banner_time"] = time();
        $where["banner_status"] = 1;
        $update = db("Banner")->insert($where);
        if (!$update) $this->ajaxReturn(603, errorMsg);
        $this->ajaxReturn(200, successMsg, url('Banner/banner'));
    }
}